using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Globalization;

namespace ASUV
{
    public class ASCompareForm : ASUV.ASChildForm
	{
		#region [members]
		private DataGridView summaryDataGridView;
		private Button closeButton;
		private Button CompareQueryButton;
		private Panel panel1;
		private Label percentagePassedLabel;
		private Label failedCountLabel;
		private Label passedCountLabel;
		private Label totalCountLabel;
		private Label totalLlabel;
		private Label passedLabel;
		private Label failedLabel;
		private Label percentageLabel;
		private GroupBox SummaryGroupBox;
        private System.ComponentModel.IContainer components = null;
		bool customize;
		ProjectFileFoundEventArgs projectDetails;
		#endregion

		#region public ASCompareForm(ProjectFileFoundEventArgs args)
		public ASCompareForm(ProjectFileFoundEventArgs args)
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
			projectDetails=args;

            // TODO: Add any initialization after the InitializeComponent call
		}
		#endregion

		#region protected override void Dispose(bool disposing)
		/// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
		}
		#endregion

		#region Designer generated code
		/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
			this.summaryDataGridView = new System.Windows.Forms.DataGridView();
			this.closeButton = new System.Windows.Forms.Button();
			this.CompareQueryButton = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.percentagePassedLabel = new System.Windows.Forms.Label();
			this.failedCountLabel = new System.Windows.Forms.Label();
			this.passedCountLabel = new System.Windows.Forms.Label();
			this.totalCountLabel = new System.Windows.Forms.Label();
			this.totalLlabel = new System.Windows.Forms.Label();
			this.passedLabel = new System.Windows.Forms.Label();
			this.failedLabel = new System.Windows.Forms.Label();
			this.percentageLabel = new System.Windows.Forms.Label();
			this.SummaryGroupBox = new System.Windows.Forms.GroupBox();
			((System.ComponentModel.ISupportInitialize)(this.summaryDataGridView)).BeginInit();
			this.panel1.SuspendLayout();
			this.SummaryGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// summaryDataGridView
			// 
			this.summaryDataGridView.AllowUserToAddRows = false;
			this.summaryDataGridView.AllowUserToDeleteRows = false;
			this.summaryDataGridView.AllowUserToOrderColumns = true;
			this.summaryDataGridView.AllowUserToResizeRows = false;
			this.summaryDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
			dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
			dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
			dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
			this.summaryDataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
			this.summaryDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.summaryDataGridView.Location = new System.Drawing.Point(19, 69);
			this.summaryDataGridView.Name = "summaryDataGridView";
			this.summaryDataGridView.ReadOnly = true;
			this.summaryDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
			this.summaryDataGridView.Size = new System.Drawing.Size(648, 434);
			this.summaryDataGridView.TabIndex = 3;
			this.summaryDataGridView.DoubleClick += new System.EventHandler(this.summaryDataGridView_DoubleClick);
			// 
			// closeButton
			// 
			this.closeButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.closeButton.Location = new System.Drawing.Point(572, 518);
			this.closeButton.Name = "closeButton";
			this.closeButton.Size = new System.Drawing.Size(95, 23);
			this.closeButton.TabIndex = 2;
			this.closeButton.Text = "Close";
			this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
			// 
			// CompareQueryButton
			// 
			this.CompareQueryButton.Enabled = false;
			this.CompareQueryButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.CompareQueryButton.Location = new System.Drawing.Point(423, 518);
			this.CompareQueryButton.Name = "CompareQueryButton";
			this.CompareQueryButton.Size = new System.Drawing.Size(125, 23);
			this.CompareQueryButton.TabIndex = 1;
			this.CompareQueryButton.Text = "Compare Query Results";
			this.CompareQueryButton.Click += new System.EventHandler(this.CompareQueryButton_Click);
			// 
			// panel1
			// 
			this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panel1.Controls.Add(this.percentagePassedLabel);
			this.panel1.Controls.Add(this.failedCountLabel);
			this.panel1.Controls.Add(this.passedCountLabel);
			this.panel1.Controls.Add(this.totalCountLabel);
			this.panel1.Controls.Add(this.totalLlabel);
			this.panel1.Controls.Add(this.passedLabel);
			this.panel1.Controls.Add(this.failedLabel);
			this.panel1.Controls.Add(this.percentageLabel);
			this.panel1.Location = new System.Drawing.Point(19, 31);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(648, 20);
			this.panel1.TabIndex = 7;
			// 
			// percentagePassedLabel
			// 
			this.percentagePassedLabel.AutoSize = true;
			this.percentagePassedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.percentagePassedLabel.Location = new System.Drawing.Point(581, 2);
			this.percentagePassedLabel.Name = "percentagePassedLabel";
			this.percentagePassedLabel.Size = new System.Drawing.Size(9, 13);
			this.percentagePassedLabel.TabIndex = 8;
			this.percentagePassedLabel.Text = "0";
			// 
			// failedCountLabel
			// 
			this.failedCountLabel.AutoSize = true;
			this.failedCountLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.failedCountLabel.Location = new System.Drawing.Point(389, 2);
			this.failedCountLabel.Name = "failedCountLabel";
			this.failedCountLabel.Size = new System.Drawing.Size(9, 13);
			this.failedCountLabel.TabIndex = 7;
			this.failedCountLabel.Text = "0";
			// 
			// passedCountLabel
			// 
			this.passedCountLabel.AutoSize = true;
			this.passedCountLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.passedCountLabel.Location = new System.Drawing.Point(249, 2);
			this.passedCountLabel.Name = "passedCountLabel";
			this.passedCountLabel.Size = new System.Drawing.Size(9, 13);
			this.passedCountLabel.TabIndex = 6;
			this.passedCountLabel.Text = "0";
			// 
			// totalCountLabel
			// 
			this.totalCountLabel.AutoSize = true;
			this.totalCountLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.totalCountLabel.Location = new System.Drawing.Point(97, 2);
			this.totalCountLabel.Name = "totalCountLabel";
			this.totalCountLabel.Size = new System.Drawing.Size(9, 13);
			this.totalCountLabel.TabIndex = 5;
			this.totalCountLabel.Text = "0";
			// 
			// totalLlabel
			// 
			this.totalLlabel.AutoSize = true;
			this.totalLlabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.totalLlabel.Location = new System.Drawing.Point(3, 2);
			this.totalLlabel.Name = "totalLlabel";
			this.totalLlabel.Size = new System.Drawing.Size(69, 13);
			this.totalLlabel.TabIndex = 1;
			this.totalLlabel.Text = "Total Queries:";
			// 
			// passedLabel
			// 
			this.passedLabel.AutoSize = true;
			this.passedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.passedLabel.Location = new System.Drawing.Point(174, 2);
			this.passedLabel.Name = "passedLabel";
			this.passedLabel.Size = new System.Drawing.Size(41, 13);
			this.passedLabel.TabIndex = 2;
			this.passedLabel.Text = "Passed:";
			// 
			// failedLabel
			// 
			this.failedLabel.AutoSize = true;
			this.failedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.failedLabel.Location = new System.Drawing.Point(330, 2);
			this.failedLabel.Name = "failedLabel";
			this.failedLabel.Size = new System.Drawing.Size(34, 13);
			this.failedLabel.TabIndex = 3;
			this.failedLabel.Text = "Failed:";
			// 
			// percentageLabel
			// 
			this.percentageLabel.AutoSize = true;
			this.percentageLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.percentageLabel.Location = new System.Drawing.Point(464, 2);
			this.percentageLabel.Name = "percentageLabel";
			this.percentageLabel.Size = new System.Drawing.Size(102, 13);
			this.percentageLabel.TabIndex = 4;
			this.percentageLabel.Text = "Percentage  Passed:";
			// 
			// SummaryGroupBox
			// 
			this.SummaryGroupBox.Controls.Add(this.panel1);
			this.SummaryGroupBox.Controls.Add(this.CompareQueryButton);
			this.SummaryGroupBox.Controls.Add(this.closeButton);
			this.SummaryGroupBox.Controls.Add(this.summaryDataGridView);
			this.SummaryGroupBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.SummaryGroupBox.Location = new System.Drawing.Point(15, 12);
			this.SummaryGroupBox.Name = "SummaryGroupBox";
			this.SummaryGroupBox.Size = new System.Drawing.Size(685, 557);
			this.SummaryGroupBox.TabIndex = 3;
			this.SummaryGroupBox.TabStop = false;
			this.SummaryGroupBox.Text = "Summary Report";
			// 
			// ASCompareForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.ClientSize = new System.Drawing.Size(709, 615);
			this.Controls.Add(this.SummaryGroupBox);
			this.Location = new System.Drawing.Point(0, 0);
			this.Name = "ASCompareForm";
			this.Text = "Comparison Result";
			this.Shown += new System.EventHandler(this.ASCompareForm_Shown);
			this.Load += new System.EventHandler(this.ASCompareForm_Load);
			((System.ComponentModel.ISupportInitialize)(this.summaryDataGridView)).EndInit();
			this.panel1.ResumeLayout(false);
			this.panel1.PerformLayout();
			this.SummaryGroupBox.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region private void ASCompareForm_Load(object sender, EventArgs e)
		private void ASCompareForm_Load(object sender, EventArgs e)
		{
			ComparisonResult();
		}
		#endregion

		#region private void ComparisonResult()
		/// <summary>
		/// Pupulate comparison result to DataGridView
		/// </summary>
		private void ComparisonResult()
		{
			try
			{
				XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				//
				//Check whether there is any query exist in the project file
				//
				if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries").HasChildNodes)
				{
					customize = true;
					DataSet ds = new DataSet();
					ds.Locale = CultureInfo.InvariantCulture;
					totalCountLabel.Text = xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText;
					passedCountLabel.Text = xmlDomProjectContent.SelectSingleNode("ProjectFile/QueriesPassed").InnerText;
					failedCountLabel.Text = xmlDomProjectContent.SelectSingleNode("ProjectFile/QueriesFailed").InnerText;
					double failedCount = double.Parse(failedCountLabel.Text.Trim(), CultureInfo.CurrentCulture);
					double passedCount = double.Parse(passedCountLabel.Text.Trim(), CultureInfo.CurrentCulture);
					double total = failedCount + passedCount;
					double passPercentage = 0;
					//
					//Calculating percentage of passed queries
					//
					if (total != 0) passPercentage = (passedCount / total) * 100;
					if (passPercentage.ToString(CultureInfo.CurrentCulture).Trim().Length <= 5)
					{
						percentagePassedLabel.Text = passPercentage.ToString(CultureInfo.CurrentCulture);
					}
					else
					{
						percentagePassedLabel.Text = passPercentage.ToString(CultureInfo.CurrentCulture).Trim().Substring(0, 5);
					}
					XmlReader xmlReadQueryList = new XmlNodeReader(xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries"));
					ds.ReadXml(xmlReadQueryList);
					ds.Tables[0].Columns.Remove("DatabaseName");
					ds.Tables[0].Columns.Remove("CubeName");
					ds.Tables[0].Columns.Remove("NumberOfCells");
					ds.Tables[0].Columns.Remove("AS2KResult");
					ds.Tables[0].Columns.Remove("AS2K5Result");
					ds.Tables[0].Columns.Remove("QueryAS2K");
					ds.Tables[0].Columns.Remove("AS2KExecDuration");
					ds.Tables[0].Columns.Remove("QueryAS2K5");
					ds.Tables[0].Columns.Remove("AS2K5ExecDuration");
					DataView dv = ds.Tables[0].DefaultView;
					dv.RowFilter = "not AS2KQueryStatus = 'Query not Executed' and not AS2K5QueryStatus = 'Query not Executed'";
					summaryDataGridView.DataSource = dv.Table;
					summaryDataGridView.Columns[0].Visible = false;
					CompareQueryButton.Enabled = true;
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
			if (summaryDataGridView.Rows.Count < 1)
			{
				CompareQueryButton.Enabled = false;
			}
		}
		#endregion

		#region private void summaryDataGridView_DoubleClick(object sender, EventArgs e)
		private void summaryDataGridView_DoubleClick(object sender, EventArgs e)
		{
			ShowResult();
		}
		#endregion

		#region private void ASCompareForm_Shown(object sender, EventArgs e)
		private void ASCompareForm_Shown(object sender, EventArgs e)
		{

			CustomizeGrid();
		}
		#endregion

		#region private void CompareQueryButton_Click(object sender, EventArgs e)
		/// <summary>
		/// Compare the query result and result populate to the DataGridView
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CompareQueryButton_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			try
			{
				XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[ComparisonResult='Same Result']") != null)
				{
					DialogResult result = MessageBox.Show("Comparison results already exist, Do you wish to destroy the old comparison result?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
					if (result == DialogResult.OK)
					{
						AnalysisService.CompareQuery(projectDetails);
						ComparisonResult();
						CustomizeGrid();
					}
				}
				else
				{
					AnalysisService.CompareQuery(projectDetails);
					ComparisonResult();
					CustomizeGrid();
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
			Cursor.Current = Cursors.Default;
		}
		#endregion

		#region private void closeButton_Click(object sender, EventArgs e)
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region private void CustomizeGrid()
		private void CustomizeGrid()
		{
			if (customize)
			{
				summaryDataGridView.Columns[0].Visible = false;
				summaryDataGridView.Columns[1].HeaderText = "Query Name";
				summaryDataGridView.Columns[2].HeaderText = "AS2K Query Status";
				summaryDataGridView.Columns[3].HeaderText = "AS2K5 Query Status";
				summaryDataGridView.Columns[4].HeaderText = "Comparison Result";
				summaryDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
				summaryDataGridView.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			}
		}
		#endregion

		#region private void ShowResult()
		private void ShowResult()
		{
			try
			{
				if (summaryDataGridView.Rows.Count != 0)
				{
					string queryID = summaryDataGridView.SelectedCells[0].Value.ToString();
					if (queryID.Trim().Length != 0)
					{
						ASQueryResultForm queryResult = new ASQueryResultForm(queryID, projectDetails);
						queryResult.ShowDialog();
					}
				}
			}
			catch
			{
			}
		}
		#endregion
	}
}

